using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;

namespace NET.Library.Data
{
    /// <summary>
    /// DBNullHelper object. Used with nullable types.
    /// </summary>
    public class DBNullHelper : DBHelper
    {
        #region Private Static Methods
        ///----------------------------------------------------------------------------------
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Object"></param>
        /// <returns></returns>
        ///----------------------------------------------------------------------------------
        //private static new DateTime? datetimeValue(object Object)
        private static new Nullable<DateTime> datetimeValue(object Object)
        {
           // return Object == DBNull.Value ? (Nullable<DateTime>)null : (DateTime?)Convert.ToDateTime(Object);
            return Object == DBNull.Value ? null : (DateTime?)Convert.ToDateTime(Object);
        }
        ///----------------------------------------------------------------------------------
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Object"></param>
        /// <returns></returns>
        ///----------------------------------------------------------------------------------
        private static new int? intValue(object Object)
        {
            return Object == DBNull.Value ? (int?)null : (int)Object;
        }
        ///----------------------------------------------------------------------------------
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Object"></param>
        /// <returns>Returns false if the object is null or the boolean value of the object.</returns>
        /// <remarks></remarks>
        /// <example></example>
        ///----------------------------------------------------------------------------------
        private static new bool? boolValue(object Object)
        {
            return Object == DBNull.Value ? (bool?)null : (bool)Object;
        }
        ///----------------------------------------------------------------------------------
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Object">Returns the default value of the DBInt enumeration as a decimal type if the object is null 
        /// or the decimal value of the object.</param>
        /// <returns></returns>
        /// <remarks></remarks>
        /// <example></example>
        ///----------------------------------------------------------------------------------
        private static new decimal? decimalValue(object Object)
        {
            return Object == DBNull.Value ? (decimal?)null : (decimal)Object;
        }

        ///----------------------------------------------------------------------------------
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Object"></param>
        /// <returns></returns>
        ///----------------------------------------------------------------------------------
        private static new double? doubleValue(object Object)
        {
            return Object == DBNull.Value ? (double?)null : (double)Object;
        }

        private static DateTime? TryParse(string text)
        {
            DateTime date;
            if (DateTime.TryParse(text, out date))
            {
                return date;
            }
            else
            {
                return null;
            }
        }

        //public static DateTime? TryParse(string text)
        //{
        //    DateTime date;
        //    return DateTime.TryParse(text, out date) ? date : (DateTime?)null;
        //}

        #endregion

        #region Static Methods
        /// *************************************************************************
        /// <summary>
        /// 
        /// </summary>
        /// <param name="IDataReader">An open IDataReader</param>
        /// <param name="FieldName">The name of the Integer field to lookup in the IDataReader.</param>
        /// <returns></returns>
        /// <remarks></remarks>
        /// <example></example>
        /// *************************************************************************
        public static new int? GetDBInt(IDataReader IDataReader, string FieldName)
        {
            //Check parameters
            CheckParam(IDataReader);
            CheckParam(FieldName);

            //Get value
            return intValue(GetDBValue(IDataReader, FieldName));

        }
        /// *************************************************************************
        /// <summary>
        /// 
        /// </summary>
        /// <param name="SqlDataReader"></param>
        /// <param name="FieldName"></param>
        /// <returns></returns>
        /// *************************************************************************
        public static new int? GetDBInt(SqlDataReader SqlDataReader, string FieldName)
        {
            //Check parameters
           CheckParam(SqlDataReader);
           CheckParam(FieldName);

            //Get value
           return intValue(GetDBValue(SqlDataReader, FieldName));
        }
        /// *************************************************************************
        /// <summary>
        /// 
        /// </summary>
        /// <param name="DataRow"></param>
        /// <param name="FieldName">The name of the field to lookup.</param>
        /// <returns></returns>
        /// <remarks></remarks>
        /// <example></example>
        /// *************************************************************************
        public static new int? GetDBInt(DataRow DataRow, string FieldName)
        {
            //Check parameters
            CheckParam(DataRow);
            CheckParam(FieldName);

            //Get value
            return intValue(GetDBValue(DataRow, FieldName));
        }
        /*************************************************************************
        ' * Name        : GetDBDatetime method
        ' * Description : Returns null if the datareader field
        ' *             : IsDBNull, otherwise returns the value
        ' *             : of the dataset field.
        ' * Syntax      : <Resp> = GetDBDatetime(<IDataReader>, <FieldName>, <dtDefault>)
        ' * Parameters  : <Resp> - the returned Date.
        ' *             : <IDataReader> - an open IDataReader.
        ' *             : <FieldName> - the name of the Date field in the datareader.
        ' *             : <dtDefault> - the default datetime value to return for null records.
        ' * Remarks     : If the value of the field is Null, the value returned is the value of dtDefault.
        ' * Example     : 
        ' *************************************************************************/
        /// <summary>
        /// 
        /// </summary>
        /// <param name="IDataReader">An open IDataReader</param>
        /// <param name="FieldName">The name of the DateTime field to lookup in the IDataReader.</param>
        /// <returns></returns>
        /// <remarks></remarks>
        /// <example>DateTime? dteExpiryDate = GetDBDatetime(IDataReaderObject, FieldName, DefaulValue)</example>
        public static new DateTime? GetDBDateTime(IDataReader IDataReader, string FieldName)//, System.DateTime dtDefault)
        {
            //Check parameters
            CheckParam(IDataReader);
            CheckParam(FieldName);

            //Get value
            return datetimeValue(GetDBValue(IDataReader, FieldName));
        }
        /*************************************************************************
        ' * Name        : GetDBDatetime method
        ' * Description : Returns null if the datareader field
        ' *             : IsDBNull, otherwise returns the value
        ' *             : of the dataset field.
        ' * Syntax      : <Resp> = GetDBDatetime(<SqlDataReader>, <FieldName>, <dtDefault>)
        ' * Parameters  : <Resp> - the returned Date.
        ' *             : <SqlDataReader> - an open SQLDataReader.
        ' *             : <FieldName> - the name of the Date field in the sqldatareader.
        ' * Remarks     : If the value of the field is Null, the value returned is the value of dtDefault.
        ' * Example     : dteExpiryDate = GetDBDatetime(SqlDataReaderObject, FieldName, DefaulValue)
        ' *************************************************************************/
        /// <summary>
        /// 
        /// </summary>
        /// <param name="SqlDataReader">An open SqlDataReader</param>
        /// <param name="FieldName">The name of the DateTime field to lookup in the SqlDataReader.</param>
        /// <returns></returns>
        /// <remarks></remarks>
        /// <example></example>
        public static new DateTime? GetDBDateTime(SqlDataReader SqlDataReader, string FieldName)//, System.DateTime dtDefault)
        {
            //Check parameters
            CheckParam(SqlDataReader);
            CheckParam(FieldName);

            //Get value
            return datetimeValue(GetDBValue(SqlDataReader, FieldName));
        }

        /// *************************************************************************
        /// <summary>
        /// 
        /// </summary>
        /// <param name="DataRow"></param>
        /// <param name="FieldName"></param>
        /// <returns></returns>
        /// *************************************************************************
        public static new DateTime? GetDBDateTime(DataRow DataRow, string FieldName)
        {
            //Check parameters
            CheckParam(DataRow);
            CheckParam(FieldName);

            //Get value
            return datetimeValue(GetDBValue(DataRow, FieldName));
        }
        /// *************************************************************************
        /// <summary>
        /// 
        /// </summary>
        /// <param name="IDataReader"></param>
        /// <param name="FieldName"></param>
        /// <returns></returns>
        /// *************************************************************************
        public static new bool? GetDBBool(IDataReader IDataReader, string FieldName)//, bool blDefault)
        {
            //Check parameters
            CheckParam(IDataReader);
            CheckParam(FieldName);

            //Get value
            return boolValue(GetDBValue(IDataReader, FieldName));
        }
        /// *************************************************************************
        /// <summary>
        /// 
        /// </summary>
        /// <param name="SqlDataReader"></param>
        /// <param name="FieldName"></param>
        /// <returns></returns>
        /// *************************************************************************
        public static bool? GetDBBool(SqlDataReader SqlDataReader, string FieldName)//, bool blDefault)
        {
            //Check parameters
            CheckParam(SqlDataReader);
            CheckParam(FieldName);

            //Get value
            return boolValue(GetDBValue(SqlDataReader, FieldName));
        }
        /// *************************************************************************
        /// <summary>
        /// 
        /// </summary>
        /// <param name="DataRow"></param>
        /// <param name="FieldName">The name of the field to lookup.</param>
        /// <returns></returns>
        /// <remarks></remarks>
        /// <example></example>
        /// *************************************************************************
        public static new bool? GetDBBool(DataRow DataRow, string FieldName)//, bool blDefault)
        {
            //Check parameters
            CheckParam(DataRow);
            CheckParam(FieldName);

            //Get value
            return boolValue(GetDBValue(DataRow, FieldName));
        }
        /// <summary>
        /// SQL Money type
        /// *************************************************************************
        /// * Name        : GetDBMoney method
        /// * Description : Returns Nothing if the datareader field
        /// *             : IsDBNull, otherwise returns the value
        /// *             : of the dataset field.
        /// * Syntax      : Resp = GetDBMoney(DataReader, Field, Default)
        /// * Parameters  : Resp        - the returned Decimal.
        /// *             : DataReader  - an open IDataReader.
        /// *             : Field       - the name of the Money field in the dataset.
        /// * Remarks     : If the value of the field is DBNull, the value returned is Nothing.
        /// * Example     : 
        ///*************************************************************************
        /// </summary>
        /// <param name="IDataReader">An open IDataReader</param>
        /// <param name="FieldName">The name of the Money field to lookup in the IDataReader.</param>
        /// <remarks></remarks>
        /// <example></example>
        public static new decimal? GetDBMoney(IDataReader IDataReader, string FieldName)//, decimal decDefault)
        {
            //Check parameters
            CheckParam(IDataReader);
            CheckParam(FieldName);

            //Get value
            return decimalValue(GetDBValue(IDataReader, FieldName));


        }
        /// <summary>
        /// SQL Money type
        /// *************************************************************************
        /// * Name        : GetDBMoney method
        /// * Description : Returns Nothing if the datareader field
        /// *             : IsDBNull, otherwise returns the value
        /// *             : of the dataset field.
        /// * Syntax      : Resp = GetDBMoney(DataReader, Field)
        /// * Parameters  : Resp        - the returned Decimal.
        /// *             : DataReader  - an open SQLDataReader.
        /// *             : Field       - the name of the Money field in the dataset.
        /// * Remarks     : If the value of the field is DBNull, the value returned is Nothing.
        /// * Example     : 
        ///*************************************************************************
        /// </summary>
        /// <param name="SqlDataReader">An open SqlDataReader</param>
        /// <param name="FieldName">The name of the Money field to lookup in the SqlDataReader.</param>
        /// <remarks></remarks>
        /// <example></example>
        public static new decimal? GetDBMoney(SqlDataReader SqlDataReader, string FieldName)
        {
            //Check parameters
            CheckParam(SqlDataReader);
            CheckParam(FieldName);

            //Get value
            return decimalValue(GetDBValue(SqlDataReader, FieldName));
        }
        /// *************************************************************************
        /// <summary>
        /// 
        /// </summary>
        /// <param name="DataRow"></param>
        /// <param name="FieldName"></param>
        /// <returns></returns>
        /// *************************************************************************
        public static decimal? GetDBMoney(DataRow DataRow, string FieldName)
        {
            //Check parameters
            CheckParam(DataRow);
            CheckParam(FieldName);

            //Get value
            return decimalValue(GetDBValue(DataRow, FieldName));
        }
        /// <summary>		
        /// SQL Decimal type
        /// *************************************************************************
        /// * Name        : GetDBDecimal method
        /// * Description : Returns Nothing if the datareader field
        /// *             : IsDBNull, otherwise returns the value
        /// *             : of the dataset field.
        /// * Syntax      : Resp = GetDBDecimal(IDataReader, FieldName)
        /// * Parameters  : Resp        - the returned Decimal.
        /// *             : IDataReader  - an open IDataReader.
        /// *             : FieldName       - the name of the Decimal field in the dataset.
        /// * Remarks     : If the value of the field is Null, the value returned is the value specified by decDefault.
        /// * Example     : 
        /// *************************************************************************
        /// </summary>
        /// <param name="IDataReader">An open IDataReader</param>
        /// <param name="FieldName">The name of the Decimal field to lookup in the IDataReader.</param>
        /// <remarks></remarks>
        /// <example></example>
        public static new decimal? GetDBDecimal(IDataReader IDataReader, string FieldName)
        {
            //Check parameters
            CheckParam(IDataReader);
            CheckParam(FieldName);

            //Get value
            return decimalValue(GetDBValue(IDataReader, FieldName));
        }
        /// <summary>		
        /// SQL Decimal type
        /// *************************************************************************
        /// * Name        : GetDBDecimal method
        /// * Description : Returns Nothing if the datareader field
        /// *             : IsDBNull, otherwise returns the value
        /// *             : of the dataset field.
        /// * Syntax      : Resp = GetDBDecimal(SqlDataReader, FieldName, decDefault)
        /// * Parameters  : Resp        - the returned Decimal.
        /// *             : SqlDataReader  - an open SQLDataReader.
        /// *             : FieldName       - the name of the Decimal field in the dataset.
        /// * Remarks     : If the value of the field is Null, the value returned is the value specified by decDefault.
        /// * Example     : 
        /// *************************************************************************
        /// </summary>
        /// <param name="SqlDataReader">An open SqlDataReader</param>
        /// <param name="FieldName">The name of the Decimal field to lookup in the SqlDataReader.</param>
        /// <remarks></remarks>
        /// <example></example>
        public static new decimal? GetDBDecimal(SqlDataReader SqlDataReader, string FieldName)
        {
            //Check parameters
            CheckParam(SqlDataReader);
            CheckParam(FieldName);

            //Get value
            return decimalValue(GetDBValue(SqlDataReader, FieldName));
        }
        /// *************************************************************************
        /// <summary>
        /// 
        /// </summary>
        /// <param name="DataRow"></param>
        /// <param name="FieldName"></param>
        /// <returns></returns>
        /// *************************************************************************
        public static new decimal? GetDBDecimal(DataRow DataRow, string FieldName)
        {
            //Check parameters
            CheckParam(DataRow);
            CheckParam(FieldName);

            //Get value
            return decimalValue(GetDBValue(DataRow, FieldName));
        }

        //Double Type
        ///----------------------------------------------------------------------------------
        /// <summary>
        /// 
        /// </summary>
        /// <param name="IDataReader"></param>
        /// <param name="FieldName"></param>
        /// <returns></returns>
        ///----------------------------------------------------------------------------------
        public static double? GetDouble(IDataReader IDataReader, string FieldName)
        {
            //Check parameters
            CheckParam(IDataReader);
            CheckParam(FieldName);

            //Get value
            return doubleValue(GetDBValue(IDataReader, FieldName));
        }

        //Double Type
        ///----------------------------------------------------------------------------------
        /// <summary>
        /// 
        /// </summary>
        /// <param name="DataRow"></param>
        /// <param name="FieldName"></param>
        /// <returns></returns>
        ///----------------------------------------------------------------------------------
        public static double? GetDouble(DataRow DataRow, string FieldName)
        {
            //Check parameters
            CheckParam(DataRow);
            CheckParam(FieldName);

            //Get value
            return doubleValue(GetDBValue(DataRow, FieldName));
        }

        ///----------------------------------------------------------------------------------
        /// <summary>
        /// 
        /// </summary>
        /// <param name="SqlDataReader"></param>
        /// <param name="FieldName"></param>
        /// <returns></returns>
        ///----------------------------------------------------------------------------------
        public static new double? GetDouble(SqlDataReader SqlDataReader, string FieldName)
        {
            //Check parameters
            CheckParam(SqlDataReader);
            CheckParam(FieldName);

            //Get value
            return doubleValue(GetDBValue(SqlDataReader, FieldName));
        }
        #endregion
    }
}
